package com.jarvis.pzz.modules.releasedemand;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.LabelList1Model;
import com.jarvis.pzz.models.RequirementInfoModel;
import com.jarvis.pzz.modules.releasedemand.adapter.ReleaseFacilitiesAdapter;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.IntentUtil;
import com.jarvis.pzz.util.net.RequestApi;
import com.jarvis.pzz.widget.ScrollGridView;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.functions.Action1;


public class ReleaseDetailsActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.lin_title)
    RelativeLayout lin_title;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.tv_details_date)
    TextView tv_details_date;
    @BindView(R.id.tv_details_title)
    TextView tv_details_title;
    @BindView(R.id.tv_details_number)
    TextView tv_details_number;
    @BindView(R.id.tv_details_name)
    TextView tv_details_name;
    @BindView(R.id.tv_details_area)
    TextView tv_details_area;
    @BindView(R.id.tv_details_budget)
    TextView tv_details_budget;
    @BindView(R.id.tv_details_operate)
    TextView tv_details_operate;
    @BindView(R.id.tv_details_district)
    TextView tv_details_district;
    @BindView(R.id.tv_details_year)
    TextView tv_details_year;
    @BindView(R.id.tv_details_demand)
    TextView tv_details_demand;
    @BindView(R.id.tv_details_transfer)
    TextView tv_details_transfer;
    @BindView(R.id.tv_details_floor)
    TextView tv_details_floor;
    @BindView(R.id.tv_details_shop)
    TextView tv_details_shop;
    @BindView(R.id.sgv_ss)
    ScrollGridView sgv_ss;
    @BindView(R.id.tv_details_collect)
    LinearLayout tv_details_collect;
    @BindView(R.id.icon_bookmark)
    ImageView icon_bookmark;
    @BindView(R.id.tv_details_instruction)
    TextView tv_details_instruction;
    @BindView(R.id.tv_release_content)
    TextView tv_release_content;
    private String number;

    private ReleaseFacilitiesAdapter releaseFacilitiesAdapter;
    private RequestService service;

    private RequirementInfoModel requirementInfoModel;
    private List<LabelList1Model> labelListBeanList;
    private String collect_status;
    String type="";


    @Override
    public int getLayoutId() {
        return R.layout.activity_release_details;
    }

    @Override
    public void initData() {


    }

    @Override
    public void initView() {
        labelListBeanList = new ArrayList<>();
        requirementInfoModel = new RequirementInfoModel();
        Intent _intent = getIntent();
        if (_intent != null) {
            number = _intent.getStringExtra("NUMBER");
            type = _intent.getStringExtra("type");
            if ("2".equals(type)) {
                tv_title.setText("求租信息");
            } else if ("1".equals(type)){
                tv_title.setText("求购信息");
            } else if("3".equals(type)){
                tv_title.setText("发布的需求详情");
            }
        }
        getRequirementInfo();
        setBar();
        setListener();
        releaseFacilitiesAdapter = new ReleaseFacilitiesAdapter(labelListBeanList, this);
        sgv_ss.setAdapter(releaseFacilitiesAdapter);

    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        rel_left.setOnClickListener(this);
        tv_details_collect.setOnClickListener(this);


    }

    public void getRequirementInfo() {
        service = RequestApi.create(RequestService.class);
        showDialog("loading");
        Call<ResponseData<RequirementInfoModel>> shopsSearchParams = service.getRequirementInfo(number);
        shopsSearchParams.enqueue(new Callback<ResponseData<RequirementInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseData<RequirementInfoModel>> call, Response<ResponseData<RequirementInfoModel>> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        requirementInfoModel = response.body().getData();
                        releaseFacilitiesAdapter.notifyDataSetChanged(requirementInfoModel.getLabelList());
                        getshopRequirementInfo(requirementInfoModel.getShopRequirementInfo());

                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData<RequirementInfoModel>> call, Throwable t) {
                disMiss();
                showTextToast(RequestApi.detailError(t));
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
            case R.id.tv_details_collect:
//                if (isLogin()) {
//                    setCollect(collect_status);
//                } else {
//                    startActivity(LoginActivity.class);
//                }
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage("400")
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(ReleaseDetailsActivity.this)
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(ReleaseDetailsActivity.this, "400");
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                dialogBuilder.create().show();
                break;

        }
    }
    private void setCollect(final String type){
        showDialog("loading");
        Call<ResponseData> responseDataCall = service.shopCollect(number, type);
        responseDataCall.enqueue(new Callback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        if("0".equals(type)){
                            collect_status="1";
                            showTextToast("已收藏");
                            tv_release_content.setText("取消收藏");
                            icon_bookmark.setImageResource(R.drawable.img_collected);
                        }else {
                            collect_status="0";
                            showTextToast("取消收藏");
                            tv_release_content.setText("收藏店铺");
                            icon_bookmark.setImageResource(R.drawable.img_un_collect);
                        }
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                disMiss();
                showTextToast(RequestApi.detailError(t));
            }
        });
    }


    private void setBar() {
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
    }

    private void  getshopRequirementInfo(RequirementInfoModel.shopRequirementInfoBean requirementInfoModel){
        tv_details_date.setText(requirementInfoModel.getReqCreatetime());
        tv_details_title.setText(requirementInfoModel.getReqTitle());
        tv_details_number.setText(requirementInfoModel.getReqNo());
        tv_details_name.setText(requirementInfoModel.getReqName());
        tv_details_area.setText(requirementInfoModel.getReqMeasureArea()+"㎡");
        tv_details_budget.setText(requirementInfoModel.getReqRent());
        tv_details_operate.setText(requirementInfoModel.getReqIndustryName());
        tv_details_district.setText(requirementInfoModel.getReqAddress());
        tv_details_year.setText(requirementInfoModel.getReqLift());
        tv_details_demand.setText(requirementInfoModel.getReqCertificates());
        tv_details_transfer.setText(requirementInfoModel.getReqTransferfee());
        tv_details_floor.setText(requirementInfoModel.getReqFloor());
        tv_details_shop.setText(requirementInfoModel.getReqShop());
        tv_details_instruction.setText(requirementInfoModel.getReqIntroduce());

        if (isLogin()) {
            collect_status=requirementInfoModel.getReqCollection();
            if ("0".equals(collect_status)) {
                tv_release_content.setText("收藏店铺");
                icon_bookmark.setImageResource(R.drawable.img_un_collect);
            } else {
                tv_release_content.setText("取消收藏");
                icon_bookmark.setImageResource(R.drawable.img_collected);
            }
        } else {
            tv_release_content.setText("收藏店铺");
            icon_bookmark.setImageResource(R.drawable.img_un_collect);
        }


    }
}
